﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeEdu.MODEL;
using LeEdu.BLL;

namespace LeEdu.Web.Controllers
{
    public class RegisterController : Controller
    {


        /// <summary>
        /// 实例化UserInfoBLL
        /// </summary>
        UserInfoBLL userbll = new UserInfoBLL();


        /// <summary>
        /// 显示注册主页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 注册方法
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public ActionResult Show()
        {
            if (userbll.getEntityList(n => true).Where(p => p.Email == Request["Email"].ToString()).Count() > 0)
            {
                return Content("<script>alert('该邮箱已被注册！');location.href='/Register/Index'</script>");
            }
            else
            {
                UserInfo user = new UserInfo();
                user.Email = Request["Email"];
                user.Upwd = Request["Upwd"];
                user.Uname = Request["Uname"];
                user.RoleId = Convert.ToInt32(Request["RoleId"]);
                //user.RoleId = 1;
                user.UTime = DateTime.Now;
                user.Tisornot = 0;
                if (user.RoleId == 2)
                {
                    user.TeacherCheck = 0;
                    if (userbll.AddEntity(user))
                    {
                        return Content("<script>alert('申请成功,管理员审核后方可登陆！！！');location.href='/Login/Index'</script>");
                    }
                    else
                    {
                        return Content("<script>alert('申请失败！！！');location.href='/Login/Index'</script>");
                    }
                }
                else if (user.RoleId == 1)
                {
                    user.TeacherCheck = 1;
                    if (userbll.AddEntity(user))
                    {
                        return Content("<script>alert('申请成功！！！');location.href='/Login/Index'</script>");
                    }
                    else
                    {
                        return Content("<script>alert('申请失败！！！');location.href='/Login/Index'</script>");
                    }
                }
                else
                {
                    return Content("<script>alert('注册失败！！！');location.href='/Register/Index'</script>");
                }
            }
        }
    }
}
